'use strict';

/* Controllers */
app.controller('companyCtrl', function($scope, ngDialog, uiLoad, rest, user, dialog, $state, uploadConf, tipService) {
  $scope.isShow = false;

  $scope.verifyFailMessage = "";
  $scope.verifying = false;
  $scope.verifyFail = false;
  $scope.verifyFail1 = false;
  $scope.agreement = true;

  $scope.requestMethodId = "4006";
  $scope.checkId = undefined;

  $scope.column2 = 0;      //分栏
  $scope.maxImgNum = 100;    //最大图片数量
  $scope.imgNew= [
      {
          text:'二房东（法人）身份证正反面',
          name: 'idCardPicChange',
          value: [],
          danger: 1
      },
      {
          text:'委托业务负责人身份证正反面',
          name: 'clientCardPicChange',
          value: []
      },
      {
          text:'营业执照',
          name: 'certificatePicChange',
          value: [],
          danger: 1
      },
      {
          text:'税务登记证与组织机构代码证',
          hink:'（三证合一可不提供）',
          name: 'codeCertificatePicChange',
          value: [],
      },
      {
          text:'股权证明（公司章程）',
          name: 'provePicChange',
          value: [],
      },
      {
          text:'二房东相关半年收租流水或者提供财报',
          hink:'（授信额可以+10%）',
          name: 'rentalPicChange',
          value: [],
          danger: 1
      }
    ];   
       
  uploadConf.column2 = $scope.column2;
  uploadConf.maxImgNum =  $scope.maxImgNum;
  uploadConf.imgNew = $scope.imgNew;

  rest.post({
    methodId: '4012',
    data: {
      landlordId: user.get('landlordId')
    },
    success: function(data) {
      if (data.code == "0") {
        var data = data.res.data;
        console.log(data);
        // status  -1=未申请 0=申请中 1=审核通过 2=审核不通过
        // identityType [0=未申请 1=业主 2=二房东（个人） 3=二房东（公司）]
        if(data.identityType == 0 || data.identityType == 3){
          if (data.status == 0) {
            $scope.steps.step2 = true;
            $scope.verifying = true;
          } else if (data.status == 1) {
            $scope.steps.step3 = true;
          } else if(data.status == -1){
            init();
            $scope.steps.step1 = true;
          } else if(data.status == 2){
            $scope.verifyFail1 = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4026";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }else if(data.status == 3){
            $scope.verifyFail = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4026";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }
        } else {
          init();
          $scope.steps.step1 = true;
        }
        $scope.isShow = true;
        $scope.$digest();
      } else {
        dialog.tips(data.res.msg);
      }
    },
    error: function() {}
  });

  $scope.update = function(){
    $scope.steps.step1 = true;
  }

  /**
   * 二房东公司
   * 4006
   */

  // 上传二房东（法人）身份证照片

  $scope.$on('idCardPicChange',function(d,data){
    $scope.data.idCard = data.list[0]
    $scope.data.idCardBack = data.list[1]
  })

  // 上传二房东业务负责人身份证

  $scope.$on('landlordClientPicChange', function(d, data) {
    $scope.data.clientCard = data.list[0];
    $scope.data.clientCardBack = data.list[1];
  });

  // 上传委托业务负责人授权书
  $scope.$on('clientPicChange', function(d, data) {
    $scope.data.entrust = data.list;
  });


  // 上传营业执照
  $scope.$on('certificatePicChange',function(d, data){
    $scope.data.certificate = data.list;
  })

  // 上传税务登记证与组织机构代码证 CodeCertificate
  $scope.$on('codeCertificatePicChange',function(d, data){
    $scope.data.codeCertificate = data.list;
  })

  // 上传股权证明
  $scope.$on('provePicChange',function(d, data){
    $scope.data.prove = data.list;
  })

  // 上传二房东相关收租流水或者提供财报
  $scope.$on('rentalPicChange',function(d, data){
    $scope.data.rental = data.list;
  })
  $scope.showTxt = function(contractId){
        ngDialog.openConfirm({
          template: 'js/module/fund/creditReport.html',
          resolve: {},
          data:{},
          scope: $scope,
          width: 800,
          height: 700
        }).then(function(data) {

        }, function(reason) {

        });
  }
  // 提交数据
  $scope.companyInfo = function() {
    $scope.verifying = false;
    $scope.verifyFail = false;
    $scope.verifyFail1 = false;

    console.log($scope.data);
    console.log(uploadConf.imgNew);

    $scope.data.idCard = {};
    $scope.data.idCardBack = {};
    $scope.data.clientCard = {};
    $scope.data.clientCardBack = {};
    $scope.data.certificate = [];
    $scope.data.codeCertificate = [];
    $scope.data.prove = [];
    $scope.data.rental = [];

    $scope.data.idCard = uploadConf.imgNew[0].value[0];
    $scope.data.idCardBack = uploadConf.imgNew[0].value[1];
    $scope.data.clientCard = uploadConf.imgNew[1].value[0];
    $scope.data.clientCardBack = uploadConf.imgNew[1].value[1];
    for (var i in uploadConf.imgNew[2].value) {
        $scope.data.certificate.push(uploadConf.imgNew[2].value[i]);
    }
    for (var i in uploadConf.imgNew[3].value) {
        $scope.data.codeCertificate.push(uploadConf.imgNew[3].value[i]);
    }
    for (var i in uploadConf.imgNew[4].value) {
        $scope.data.prove.push(uploadConf.imgNew[4].value[i]);
    }
    for (var i in uploadConf.imgNew[5].value) {
        $scope.data.rental.push(uploadConf.imgNew[5].value[i]);
    }

    for (var i in $scope.imgNew) {
        if ($scope.imgNew[0].value.length < 2) {
          tipService.show("body", {
              message: "业主（房东）身份证正反面照片必传",
              notice: "danger"
          }, 3);
          return;
        }
        if ($scope.imgNew[2].value.length < 1) {
          tipService.show("body", {
              message: "营业执照照片必传",
              notice: "danger"
          }, 3);
          return;
        }
        // if ($scope.imgNew[3].value.length < 1) {
        //   tipService.show("body", {
        //       message: "税务登记证与组织机构代码证必传",
        //       notice: "danger"
        //   }, 3);
        //   return;
        // }
        if ($scope.imgNew[5].value.length < 1) {
          tipService.show("body", {
              message: "二房东相关半年收租流水或者提供财报必传",
              notice: "danger"
          }, 3);
          return;
        }
    }
    if ($scope.data.phone.length != 11 || $scope.data.phone.charAt(0) != 1) {
        tipService.show("body", {
            message: "法人手机号格式输入错误",
            notice: "danger"
        }, 3);
        return;
    }
    if ($scope.data.clientPhone) {
        if ($scope.data.clientPhone.length != 11 || $scope.data.clientPhone.charAt(0) != 1) {
          tipService.show("body", {
              message: "业务负责人手机号格式输入错误",
              notice: "danger"
          }, 3);
          return;
        }
    }
    $scope.data.landlordName = $scope.data.landlordName.replace(/[ 　\[\]\\\/\"\'\-!@#$%^&*(){}<>,.?:;~`_+=]/g, "");
    
    rest.post({
      methodId: $scope.requestMethodId,
      data: $scope.data,
      success: function(data) {
        if (data.code == "0") {
          $scope.steps.step2 = true;
          $scope.verifying = true;
          $scope.$digest();
        } else {
          dialog.tips(data.res.msg);
        }
      },
      error: function() {}
    });
  }

  $scope.goHouseEdit = function () {
    // http://120.26.246.115:8020/index.html?landlordId=10039
    if (user.get('landlordId')) {
      $state.go('app.houseOwner.list');
      // location.href = "http://120.26.246.115:8020/index.html?landlordId=" + user.get('landlordId');
    }
    // console.log('goHouseEdit: %s', user.get('landlordId'));
  }

  $scope.goBack = function(){
    $state.go('nav.widget')
  }

  $scope.showidCardPicTips = function(){
    dialog.tips("身份证拍照必须正反面清晰，无光线遮挡，无缺角与遮挡。（纸质与影像件）")
  }
  $scope.showlandlordClientPicTips = function(){
    dialog.tips("身份证拍照必须正反面清晰，无光线遮挡，无缺角与遮挡。（纸质与影像件）")
  }
  $scope.showclientPicTips = function(){
    dialog.tips("")
  }
  $scope.showcertificatePicTips = function(){
    dialog.tips("")
  }
  $scope.showcodeCertificatePicTips = function(){
    dialog.tips("")
  }
  $scope.showprovePicTips = function(){
    dialog.tips("申请人的项目股权占比75%上")
  }
  $scope.showrentalPicTips = function(){
    dialog.tips("提供的流水必须是房东的业务资金来往流水，流水金额能明确体现出企业的房租支付，水电煤物业费支付，房客租金收入等情况。（纸质与影像件）银行EXCEL表格无效。企业可以提供对外财务报表。")
  }

  function init(data){
    $scope.data = {
      landlordId : user.get('landlordId'),
      landlordName: !!data ? data.landlordName : "",
      idCardNo: !!data ? data.idCardNo : "",
      bankName: !!data ? data.bankName : "",
      bankAccount: !!data ? data.bankAccount : "",
      phone: !!data ? data.phone : "",
      clientPhone: !!data ? data.clientPhone : "",
      company: !!data ? data.company : "",
      companyAddress: !!data ? data.companyAddress : "",
      idCard: !!data ? data.idCard : "",
      idCardBack: !!data ? data.idCardBack : "",
      clientCard: !!data ? data.clientCard : "",
      clientCardBack: !!data ? data.clientCardBack : "",
      entrust: !!data ? data.entrust : "",
      certificate: !!data ? data.certificate : "",
      codeCertificate: !!data ? data.codeCertificate : "",
      prove: !!data ? data.prove : "",
      rental: !!data ? data.rental : "",
      checkId: $scope.checkId
    };
    if (data) {
      // 上传二房东（法人）身份证照片
      $scope.imgNew[0].value = [];
      if (data.idCard)
          $scope.imgNew[0].value.push(data.idCard);
      if (data.idCardBack)
          $scope.imgNew[0].value.push(data.idCardBack);
      // 委托业务负责人身份证
      $scope.imgNew[1].value = [];
      if (data.clientCard)
          $scope.imgNew[1].value.push(data.clientCard);
      if (data.clientCardBack)
          $scope.imgNew[1].value.push(data.clientCardBack);
      // 上传营业执照
      $scope.imgNew[2].value = data.certificate;
      // 上传税务登记证与组织机构代码证 CodeCertificate
      $scope.imgNew[3].value = data.codeCertificate;
      // 上传股权证明
      $scope.imgNew[4].value = data.prove;
      // 上传二房东相关收租流水或者提供财报
      $scope.imgNew[5].value = data.rental;
    }
    // $scope.idCardPic = !!data ? [data.idCard, data.idCardBack] : [];
    // $scope.landlordClientPic = !!data ? [data.clientCard, data.clientCardBack] : [];

    // $scope.clientPic = (data && data.entrust) ? data.entrust : [];

    // $scope.certificatePic = (data && data.certificate) ? data.certificate : [];

    // $scope.codeCertificatePic = (data && data.codeCertificate) ? data.codeCertificate : [];

    // $scope.provePic = (data && data.prove) ? data.prove : [];

    // $scope.rentalPic = (data && data.rental) ? data.rental : [];

  }
  function getPersonalInfo(){
    rest.post({
      methodId: '4025',
      data: {
        id: $scope.checkId
      },
      success: function(data) {
        if (data.code == "0") {
          init(data.res.data);
        } else {
          dialog.tips(data.res.msg);
        }
      },
      error: function() {}
    });
  }

});
